﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    using global::WatiN.Core;

    public class NewsletterPage : WatiNBase
    {
        public NewsletterPage(WatiNBase watinBase)
            : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName)
        {

        }

        #region TextFields
        public TextField AdditionalEmailsField
        {
            get { return IEInstance.TextField(Find.ById(s => s.EndsWith("txtEmail"))); }
        }
        public TextField FromAddressField
        {
            get { return IEInstance.TextField(Find.ById(s => s.EndsWith("txtFrom"))); }
        }
        public TextField ReplyToField
        {
            get { return IEInstance.TextField(Find.ById(s => s.EndsWith("txtReplyTo"))); }
        }
        public TextField SubjectField
        {
            get { return IEInstance.TextField(Find.ById(s => s.EndsWith("txtSubject"))); }
        }
        public TextField ContentTextBox
        {
            get { return IEInstance.TextField(Find.ById(new Regex(".*Message_txtDesktopHTML", RegexOptions.IgnoreCase))); }
        }
        #endregion

        #region Links
        public Link SendButton
        {
            get { return IEInstance.Link(Find.ByTitle("Send Email")); }
        }
        public Link AdvancedSettingTab
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Advanced Settings"))); }
        }
        public Link MessageTabLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Message"))); }
        }
        public Link AdvancedSettingsTabLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Advanced Settings"))); }
        }
        #endregion

        #region Images
        public Image AdvancedSettingsExpandIcon
        {
            get { return IEInstance.Image(Find.ById(s => s.EndsWith("Newsletter_dshAdvanced_imgIcon"))); }
        }
        #endregion

        #region CheckBoxes
        public CheckBox ReplaceTokensCheckBox
        {
            get { return IEInstance.CheckBox(Find.ById(s => s.EndsWith("Newsletter_chkReplaceTokens"))); }
        }
        #endregion

        #region SelectLists
        public SelectList SendMethodSelectList
        {
            get { return IEInstance.SelectList(Find.ById(s => s.EndsWith("Newsletter_cboSendMethod"))); }
        }
        #endregion

        #region Methods
        public void SelectRadioButtonByName(IE ieInstance, string selectedValue)
        {
            var radioButtons = ieInstance.RadioButtons;

            foreach (var rb in radioButtons)
            {
                if (selectedValue != rb.GetAttributeValue("value"))
                {
                    continue;
                }

                rb.Checked = true;
                break;
            }
        }

        public void CheckRoleCheckBoxByName(string roleName)
        {
            //Returns the Edit Button for the user specified
            CheckBox roleCheckBox = null;
            TableRowCollection roleRows = IEInstance.Div(Find.ByClass("dnnRolesGrid")).Table(Find.Any).TableRows;
            foreach (TableRow row in roleRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(roleName))
                {
                    roleCheckBox = row.TableCells[1].CheckBox(Find.Any);
                }
                continue;
            }
            roleCheckBox.Checked = true;
        }
        #endregion

    }
}
